//package com.tduck.cloud.poster.service.impl;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.core.map.MapUtil;
//import cn.hutool.core.util.IdUtil;
//import cn.hutool.core.util.StrUtil;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.freewayso.image.combiner.ImageCombiner;
//import com.tduck.cloud.common.util.JsonUtils;
//import com.tduck.cloud.common.util.Result;
//import com.tduck.cloud.form.entity.UserFormDataEntity;
//import com.tduck.cloud.form.service.UserFormDataService;
//import com.tduck.cloud.form.service.UserFormItemService;
//import com.tduck.cloud.form.vo.FormFieldVO;
//import com.tduck.cloud.poster.entity.PosterEntity;
//import com.tduck.cloud.poster.entity.PosterFormLinkEntity;
//import com.tduck.cloud.poster.mapper.PosterFormLinkMapper;
//import com.tduck.cloud.poster.service.PosterFormLinkService;
//import com.tduck.cloud.poster.service.PosterService;
//import com.tduck.cloud.poster.util.ImageCombinerBuilder;
//import com.tduck.cloud.storage.cloud.OssStorageFactory;
//import lombok.RequiredArgsConstructor;
//import lombok.SneakyThrows;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//
///**
// * 海报设计器单关联Service业务层处理
// *
// * @author tduck-gen
// * @date 2023-02-12 17:06:53
// */
//@Service
//@RequiredArgsConstructor
//public class PosterFormLinkServiceImpl extends ServiceImpl<PosterFormLinkMapper, PosterFormLinkEntity> implements PosterFormLinkService {
//    private final PosterService posterService;
//    private final UserFormDataService userFormDataService;
//
//    private final UserFormItemService userFormItemService;
//
//    @SneakyThrows
//    @Override
//    public Result<Map<String, Object>> generate(String linkKey, Long dataId) {
//        PosterFormLinkEntity formLinkEntity = this.getOne(Wrappers.<PosterFormLinkEntity>lambdaQuery()
//                .eq(PosterFormLinkEntity::getLinkKey, linkKey));
//        if (null == formLinkEntity) {
//            return Result.failed("错误的请求");
//        }
//        String posterKey = formLinkEntity.getPosterKey();
//        PosterEntity posterEntity = posterService.getOne(Wrappers.<PosterEntity>lambdaQuery().eq(PosterEntity::getPosterKey, posterKey));
//        if (null == posterEntity) {
//            return Result.failed("不存在该海报");
//        }
//        // 查询数据Id
//        UserFormDataEntity formData = userFormDataService.getById(dataId);
//        if (null == formData) {
//            return Result.failed("不存在该数据");
//        }
//        // 生成海报
//        List<FormFieldVO> fields = userFormItemService.listFormFields(formData.getFormKey());
//        Map<String, Object> schemeMap = fields.stream().collect(HashMap::new, (k, v) -> k.put(v.getValue(), v.getScheme()), HashMap::putAll);
//        // 对应的组件定义
//        Map<String, Object> originalData = formData.getOriginalData();
//        formData.setOriginalData(null);
//        originalData.putAll(BeanUtil.beanToMap(formData, false, true));
//        // 格式化数据
//        HashMap<Object, Object> formatValueMap = MapUtil.newHashMap();
////        for (String key : originalData.keySet()) {
////            formatValueMap.put(key, FormatDataContext.getHandler(key).formatData(key, originalData.get(key), schemeMap.get(key)));
////        }
//        // 替换映射关系中的变量
//        Map<String, String> repalceValueMap = MapUtil.newHashMap();
//        // 泛型擦除了 无法直接使用
//        List<PosterFormLinkEntity.Field> fieldList = JsonUtils.jsonToList(JsonUtils.objToJson(formLinkEntity.getFields()), PosterFormLinkEntity.Field.class);
//        for (PosterFormLinkEntity.Field field : fieldList) {
//            String dynamicValue = field.getDynamicValue();
//            // 替换变量
//            for (Map.Entry<Object, Object> entry : formatValueMap.entrySet()) {
//                dynamicValue = dynamicValue.replace("${" + entry.getKey() + "}", entry.getValue().toString());
//            }
//            repalceValueMap.put(field.getValue(), dynamicValue);
//        }
//        ImageCombiner imageCombiner = ImageCombinerBuilder.build(posterEntity, repalceValueMap);
//        String imageUrl = ImageCombinerBuilder.uploadImageUrl(posterKey, imageCombiner.getCombinedImageStream());
//        Map<String, Object> result = MapUtil.of("descText", formLinkEntity.getDescText());
//        result.put("imageUrl", imageUrl);
//        return Result.success(result);
//    }
//}
